Return to start page
Core/Maths/Library Rect.j
1 library ALibraryCoreMathsRect
2
3 /**
4 * @return Returns the rect where rect @param rect0 and @param rect1 cross each other. If both rects do not cross anywhere function will return null.
5 */
6 function GetRectsBorderRect takes rect rect0, rect rect1 returns rect
7 local real x0 = RMaxBJ(GetRectMinX(rect0), GetRectMinX(rect1))
8 local real x1 = RMinBJ(GetRectMaxX(rect0), GetRectMaxX(rect1))
9 local real y0 = RMaxBJ(GetRectMinY(rect0), GetRectMinY(rect1))
10 local real y1 = RMinBJ(GetRectMaxY(rect0), GetRectMaxY(rect1))
11 if ((x0 > x1) or (y0 > y1)) then
12 return null
13 endif
14 return Rect(x0, y0, x1, y1)
15 endfunction
16
17 /**
18 * @return Returns a rect at point with coordinates @param x and @param y and with width @param width and height @param height.
19 * @see RectFromCenterSizeBJ
20 */
21 function RectFromPointSize takes real x, real y, real width, real height returns rect
22 return Rect((x - width * 0.5), (y - height * 0.5), (x + width * 0.5), (y + height * 0.5))
23 endfunction
24
25 function SetRectByCoordinates takes rect usedRect, real x1, real y1, real x2, real y2, real x3, real y3, real x4, real y4 returns nothing
26 local real minX = 0.0
27 local real maxX = 0.0
28 local real minY = 0.0
29 local real maxY = 0.0
30
31 if (x1 >= x2 and x1 >= x3 and x1 >= x4) then
32 set maxX = x1
33 elseif (x2 >= x1 and x2 >= x3 and x2 >= x4) then
34 set maxX = x2
35 elseif (x3 >= x1 and x3 >= x2 and x3 >= x4) then
36 set maxX = x3
37 else
38 set maxX = x4
39 endif
40
41 if (x1 <= x2 and x1 <= x3 and x1 <= x4) then
42 set minX = x1
43 elseif (x2 <= x1 and x2 <= x3 and x2 <= x4) then
44 set minX = x2
45 elseif (x3 <= x1 and x3 <= x2 and x3 <= x4) then
46 set minX = x3
47 else
48 set minX = x4
49 endif
50
51 if (y1 >= y2 and y1 >= y3 and y1 >= y4) then
52 set maxY = y1
53 elseif (y2 >= y1 and y2 >= y3 and y2 >= y4) then
54 set maxY = y2
55 elseif (y3 >= y1 and y3 >= y2 and y3 >= y4) then
56 set maxY = y3
57 else
58 set maxY = y4
59 endif
60
61 if (y1 <= y2 and y1 <= y3 and y1 <= y4) then
62 set minY = y1
63 elseif (y2 <= y1 and y2 <= y3 and y2 <= y4) then
64 set minY = y2
65 elseif (y3 <= y1 and y3 <= y2 and y3 <= y4) then
66 set minY = y3
67 else
68 set minY = y4
69 endif
70
71 call SetRect(usedRect, minX, minY, maxX, maxY)
72 endfunction
73
74 function GetGroupInRectByCoordinates takes real x1, real y1, real x2, real y2, real x3, real y3, real x4, real y4 returns group
75 local rect usedRect = Rect(0.0, 0.0, 0.0, 0.0)
76 local group usedGroup = CreateGroup()
77 local unit firstUnit = null
78 local real firstUnitX = 0.0
79 local real firstUnitY = 0.0
80 call SetRectByCoordinates(usedRect, x1, y1, x2, y2, x3, y3, x4, y4)
81 call GroupEnumUnitsInRect(usedGroup, usedRect, null)
82 loop
83 set firstUnit = FirstOfGroup(usedGroup)
84 exitwhen (firstUnit == null)
85
86 set firstUnitX = GetUnitX(firstUnit)
87 set firstUnitY = GetUnitY(firstUnit)
88
89 if ((firstUnitY - y1) * (x2 - x1) - (firstUnitX - x1) * (y2 - y1) > 0.0) then
90 call GroupRemoveUnit(usedGroup, firstUnit)
91 elseif ((firstUnitY - y2) * (x3 - x2) - (firstUnitX - x2) * (y3 - y2) > 0.0) then
92 call GroupRemoveUnit(usedGroup, firstUnit)
93 elseif ((firstUnitY - y3) * (x4 - x3) - (firstUnitX - x3) * (y4 - y3) > 0.0) then
94 call GroupRemoveUnit(usedGroup, firstUnit)
95 elseif ((firstUnitY - y4) * (x1 - x4) - (firstUnitX - x4) * (y1 - y4) > 0.0) then
96 call GroupRemoveUnit(usedGroup, firstUnit)
97 endif
98 set firstUnit = null
99 endloop
100 call RemoveRect(usedRect)
101 set usedRect = null
102 return usedGroup
103 endfunction
104
105 endlibrary